<?php 
function login_db() {
	if(count($_POST)==0) {
		require("view/home.tpl");
	} else {
		require("model/userBD.php");
		$nom = authentification($_POST['mail'], $_POST['password']);
		if($nom != "" ) {
			header("Location:index.php?page=home");
		} else {
			$message = "Mail et/ou mot de passe incorrect. Veuillez reessayer.";
			require("view/error.tpl");
		}
	}
}
function register() {
			
	if(count($_POST)==0) {
		require("view/register.tpl");
	} else {
		require("Model/userBD.php");
		if(inscriptionBD($_POST['nom'], $_POST['prenom'], $_POST['mail'], $_POST['password'], $_POST['civilite'], $_POST['birthday'], $_POST['country'], $_POST['city'], $_POST['tel_number'], $_POST['droit'])) {
			$message = "Inscription Validee. Veuillez vous connecter.";
			include('mail/email.php');
			if(registrationMail($_POST['mail'],$_POST['civilite'].' '.strtoupper($_POST['nom']),$_POST['mail'],$_POST['password']) == 1)
				$message .=" Un mail vous a ete envoye";
			require("view/valid.tpl");
		} else {
			$message = "Mail deja existant.";
			require("view/register.tpl");
		}
	}
}

function disconnect() {
	$message = "A bientot !";
	unset($_SESSION);
	unset($_COOKIE);
	session_destroy();
	header("Location:index.php?page=home");
}

function user_view() {
	require("model/userBD.php");
	$tab = select_user();
	if(sizeof($tab['id_user']) > 0) {
		require("view/admin_user.tpl");
	} else {
		$message = "Aucune categorie disponible pour le moment";
		require("view/admin_user.tpl");
	}
}

function user_view_pagination($page) {
	require("model/userBD.php");
	$tab = select_user_pagination($page);
	if(sizeof($tab['id_user']) > 0) {
		require("view/admin_user.tpl");
	} else {
		$message = "Aucune categorie disponible pour le moment";
		require("view/admin_user.tpl");
	}
}


function delete_user($id_user) {
	if (isset($id_user)) {
		require("model/userBD.php");
		if (delete_user_db($id_user)) {
			$message = "Utilisateur supprime";
			require("view/valid.tpl");
		} else {
			require("view/admin_user.tpl");
		}
	}
}

function admin_user_view_by_id($id_user) {
	require("model/userBD.php");
	$tab = admin_select_user_by_id($id_user);
	if(sizeof($tab['id_user']) > 0) {
		$display = "block";
		require("view/admin_user.tpl");
	} else {
		$message = "Aucun utilisateur disponible pour le moment";
		require("view/home.tpl");
	}
}

function user_view_by_id($id_user) {
	require("model/userBD.php");
	$tab = select_user_by_id($id_user);
	if(sizeof($tab['id_user']) > 0) {
		$display = "block";
		require("view/my_account.tpl");
	} else {
		$message = "Aucun utilisateur disponible pour le moment";
		require("view/home.tpl");
	}
}

function admin_update_user($id_user, $mail, $civilite, $nom, $prenom, $password, $droit) {
		if (isset($id_user) && isset($mail) && isset($civilite) && isset($nom) && isset($prenom) && isset($password) && isset($droit)) {
		require("model/userBD.php");
		if (admin_update_user_db($id_user, $mail, $civilite, $nom, $prenom, $password, $droit)) {
			$message = "Utilisateur modifie";
			require("view/valid.tpl");
		} else {
			require("view/admin_user.tpl");
		}
	}
}

function update_user($id_user, $civilite, $nom, $prenom, $password, $country, $city, $tel_number, $nom_image) {
		if (isset($id_user) && isset($civilite) && isset($nom) && isset($prenom) && isset($password)) {
		require("model/userBD.php");
		if (update_user_db($id_user, $civilite, $nom, $prenom, $password, $country, $city, $tel_number, $nom_image)) {
			$message = "Votre compte a ete mis a jour";
			require("view/valid.tpl");
		} else {
			require("view/my_account.tpl");
		}
	}
}

?>